package com.example.myjavafx;

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.geometry.Point3D;
import javafx.scene.*;
import javafx.scene.chart.BarChart;
import javafx.scene.chart.CategoryAxis;
import javafx.scene.chart.NumberAxis;
import javafx.scene.chart.XYChart;
import javafx.scene.control.Button;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Box;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;

/**
 * @author admin
 */
public class D2D3DSwitchDemo extends Application {

    // 共享数据源
    private final String[] months = {"Jan", "Feb", "Mar"};
    private final double[] sales = {150, 230, 180};

    private BorderPane root = new BorderPane();
    private Group threeDGroup = new Group();
    private PerspectiveCamera camera = new PerspectiveCamera(true);
    private SubScene subScene3D;

    @Override
    public void start(Stage primaryStage) {
        // 创建切换按钮
        HBox controlBox = new HBox(10);
        Button btn2D = new Button("2D视图");
        Button btn3D = new Button("3D视图");
        controlBox.getChildren().addAll(btn2D, btn3D);
        controlBox.setPadding(new Insets(10));

        // 初始化3D场景
        init3DScene();

        // 默认显示2D视图
        show2DView();

        // 按钮事件绑定
        btn2D.setOnAction(e -> show2DView());
        btn3D.setOnAction(e -> show3DView());

        root.setTop(controlBox);

        Scene mainScene = new Scene(root, 800, 600);
        mainScene.setCamera(new PerspectiveCamera()); // 防止2D变形

        primaryStage.setTitle("2D/3D视图切换演示");
        primaryStage.setScene(mainScene);
        primaryStage.show();
    }

    // 初始化3D场景
    private void init3DScene() {
        // 创建3D坐标系
        Group axisGroup = create3DAxes();

        // 创建数据立方体
        for(int i=0; i<months.length; i++){
            Box bar = new Box(50, sales[i], 50);
            bar.setMaterial(new PhongMaterial(getColor(i)));
            bar.setTranslateX(i * 100 - 150);
            bar.setTranslateY(-sales[i]/2); // 底部对齐
            threeDGroup.getChildren().add(bar);
        }

        // 组合3D元素
        Group full3DGroup = new Group(axisGroup, threeDGroup);

        // 设置3D子场景
        subScene3D = new SubScene(full3DGroup, 800, 600, true, SceneAntialiasing.BALANCED);
        subScene3D.setCamera(camera);
        setupCameraControls();
    }

    // 创建3D坐标系
    private Group create3DAxes() {
        Group axisGroup = new Group();

        // X轴（红色）
        Box xAxis = new Box(400, 2, 2);
        xAxis.setMaterial(new PhongMaterial(Color.RED));
        xAxis.setTranslateY(150);

        // Y轴（绿色）
        Box yAxis = new Box(2, 300, 2);
        yAxis.setMaterial(new PhongMaterial(Color.GREEN));

        // Z轴（蓝色）
        Box zAxis = new Box(2, 2, 400);
        zAxis.setMaterial(new PhongMaterial(Color.BLUE));
        zAxis.setTranslateY(150);
        zAxis.setRotationAxis(new Point3D(1,0,0));
        zAxis.setRotate(90);

        axisGroup.getChildren().addAll(xAxis, yAxis, zAxis);
        return axisGroup;
    }

    // 显示2D视图
    private void show2DView() {
        BarChart<String, Number> chart = create2DBarChart();
        root.setCenter(chart);
        if(subScene3D != null) subScene3D.setManaged(false);
    }

    // 显示3D视图
    private void show3DView() {
        root.setCenter(subScene3D);
        subScene3D.setManaged(true);
        resetCamera();
    }

    // 创建2D柱状图
    private BarChart<String, Number> create2DBarChart() {
        CategoryAxis xAxis = new CategoryAxis();
        xAxis.setLabel("月份");
        NumberAxis yAxis = new NumberAxis();
        yAxis.setLabel("销售额");

        BarChart<String, Number> barChart = new BarChart<>(xAxis, yAxis);
        barChart.setTitle("季度销售数据");

        XYChart.Series<String, Number> series = new XYChart.Series<>();
        for(int i=0; i<months.length; i++){
            series.getData().add(new XYChart.Data<>(months[i], sales[i]));
        }
        barChart.getData().add(series);
        return barChart;
    }

    // 3D场景相机控制
    private void setupCameraControls() {
        Rotate xRotate = new Rotate(0, Rotate.X_AXIS);
        Rotate yRotate = new Rotate(0, Rotate.Y_AXIS);

        subScene3D.setOnMouseDragged(e -> {
            if(e.isPrimaryButtonDown()) {
                yRotate.setAngle(yRotate.getAngle() + e.getX()/2);
                xRotate.setAngle(xRotate.getAngle() - e.getY()/2);
            }
        });

        camera.getTransforms().addAll(xRotate, yRotate);
        camera.setTranslateZ(-500);
    }

    // 重置相机位置
    private void resetCamera() {
        camera.getTransforms().clear();
        camera.setTranslateZ(-500);
        camera.setRotationAxis(Rotate.X_AXIS);
        camera.setRotate(0);
    }

    // 生成不同颜色材质
    private Color getColor(int index) {
        return switch (index % 3) {
            case 0 -> Color.CORNFLOWERBLUE;
            case 1 -> Color.LIGHTGREEN;
            case 2 -> Color.SALMON;
            default -> Color.GRAY;
        };
    }

    public static void main(String[] args) {
        launch(args);
    }
}
